app.controller 'ActivityListController', [
	'$scope', 'Activity', 'activityList', '$location'
	,($scope, Activity, activityList, $location)->
		$scope.activityList = activityList
		$scope.activityListClass = 'active'
		$scope.detailView = (activity)->
			$location.path '/activity/detail/'+ activity.id
]

app.controller 'ActivityDetailController', [
	'$scope', 'Activity', 'activity', '$timeout', '$alert', '$location'
	,($scope, Activity, activity, $timeout, $alert, $location)->
		$scope.activityListClass = 'active'
		$scope.activity = activity
		$scope.save = ()->
			new Activity($scope.activity).$save {id:activity.id}, ()->
				$alert({title:'保存成功', placement:'top-right',type:'info',show:true,duration:2})
		$scope.delete = ()->
			return unless confirm('确定删除此活动')
			new Activity($scope.activity).$delete {id:activity.id}, ()->
				alert = $alert({title:'删除成功', placement:'top-right',type:'info',show:true,duration:2})
				$timeout ()->
					alert.hide()
					$location.path '/activity/list'
				,2000
]

app.controller 'ActivityNewController', [
	'$scope', 'Activity', '$alert', '$timeout', '$location'
	,($scope, Activity, $alert, $timeout, $location)->
		$scope.activityNewClass = 'active'
		$scope.save = ()->
			new Activity($scope.activity).$save ()->
				alert = $alert({title: '保存成功', content: '准备跳转到列表页',placement: 'top-right', type:'info', show:true}) 
				$timeout ()->
					alert.hide()
					$location.path '/activity/list'
				,2000
]
app.controller 'ActivitySignController', [
	'$scope', 'activitySignList', 'Activity', '$alert', '$timeout', '$location'
	,($scope, activitySignList, Activity, $alert, $timeout, $location)->
		$scope.activityListClass = 'activity'
		$scope.activitySignList = activitySignList
]
app.config ['$routeProvider', ($routeProvider)->
	$routeProvider.when '/activity/list',
		templateUrl: '/admin/tmpl/activity_list'
		controller: 'ActivityListController'
		resolve:
			activityList: (Activity)->
				Activity.query()

	$routeProvider.when '/activity/new',
		templateUrl: '/admin/tmpl/activity_new'
		controller: 'ActivityNewController'

	$routeProvider.when '/activity/detail/:id',
		templateUrl : '/admin/tmpl/activity_detail'
		controller : 'ActivityDetailController'
		resolve : 
			activity: (Activity, $route)->
				Activity.get {id: $route.current.params.id}
	$routeProvider.when '/activity/:id/sign',
		templateUrl: '/admin/tmpl/activity_sign'
		controller: 'ActivitySignController'
		resolve : 
			activitySignList: (Activity, $route)->
				Activity.getSignList($route.current.params.id)
	$routeProvider.when '/activity',
		redirectTo : '/activity/list'
]